Installation Node.js
Windows:
In order to start, go to the download page Node.js (https://nodejs.org/en/download/). Select the installer that you want to download and install.
After we run the installer and complete the installation step-by-step.
CentOS:
To install Node.js, you need to connect to the server using the SSH protocol. After that, you need to install the latest version of .nodejs from the repositories. For this:
1 | $ yum update |
Node.js is installed. By default, its binary code is in /usr/local/bin/node.
Ubuntu
To install Node and npm via apt-get, run these commands:
1 | sudo apt-get update |
If you still want to use apt-get, but need a much newer version of Node, you should use this method.
This is very similar to the last one I showed you, but instead we’ll be running a script (maintained and distributed by NodeSource) to show the package manager where to get the latest version.
1 |
|
$ node –version
1 |
|
$ node1
2
3
4
5
### Setting dependencies.
You can interactively generate the * package.json * file with the * npm init * command in the terminal.
$ npm init
1 |
|
npm install[module name] –save
1 |
|
$ sudo rpm -Uvh http://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm
1 | After the repository is added to your system, you can proceed to install the program. |
$ sudo yum install postgresql96-server postgresql96
1 |
|
$ /usr/pgsql-9.6/bin/postgresql96-setup initdb1
2
3
4
5
6
7
**Ubuntu**
However, in PPA developers PostgreSQL you can find the latest version.
Add the PostgreSQL repository to the system source list:
$ sudo sh -c ‘echo “deb http://apt.postgresql.org/pub/repos/apt/ lsb_release -cs
-pgdg main” >> /etc/apt/sources.list.d/pgdg.list’
1 |
|
$ wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
1 |
|
initdb -U postgres -D data
pg_ctl -D data start
psql -U postgres
1
2
3
4
5
Here *postgres* is the user name, it's better to specify it by default, for starters, and *data* is the directory where databases will be stored, you can specify any convenient.
After that *pg_ctl* starts PostgreSQL (indicating the directory with the databases).
At the end, we should see a welcome console:
postgres=#1
2
3
4
### Express installing.
To install Express and add it to the dependency list, you must go to the project folder and run the command:
$ npm install express –save
1 |
|
$ mkdir nodejs-express-postgresql/
$ cd nodejs-express-postgresql/
1 |
|
$ npm init -y
1 |
|
$ touch app.js
1 |
|
const express = require(‘express’);
const bodyParser = require(‘body-parser’);
// Set up the express app
const app = express();
// Setup a default catch-all route that sends back a welcome message in JSON format.
app.get(‘*’, (req, res) => res.status(200).send({
message: ‘Welcome, my frined.’,
}));
module.exports = app;
1 |
|
npm run start:dev
1 |
|
const Pool = require(‘pg’).Pool;
1 |
|
const config = {
user: ‘postgres’,
host: ‘localhost’,
database: ‘users’,
password: ‘’,
};
const pool = new Pool(config);
1 |
|
(async () => {
const client = await pool.connect()
try {
const res = await pool.query(“select * from users”);
console.log(res.rows[0])
} finally {
client.release()
}
})().catch(e => console.log(e.stack))
```
Now when you reboot our server you can see the result.
Materials and useful links that are used in the work.
PostgreSQL:
https://node-postgres.com/
http://expressjs.com/ru/guide/database-integration.html
Node.js:
https://nodejs.org/en/
https://medium.com/devschacht/node-hero-chapter-1-239f7afeb1d1
https://node-postgres.com/features/pooling
Express:
http://expressjs.com/ru/